﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using TakaGUI.Services;

namespace TakaGUI.DrawBoxes
{
	public class SwitchBox : SingleSlotBox
	{
		public DrawBox SelectedDrawBox
		{
			get;
			private set;
		}

		public void Initialize()
		{
			base.BaseInitialize();
		}

		protected override void DrawBoxHasBeenAdded(DrawBox box, SlotBox.SlotHandler handler)
		{
			SelectDrawBox(box);
		}
		protected override void DrawBoxHasBeenRemoved(DrawBox box, SlotBox.SlotHandler handler)
		{
			if (DrawBoxList.Count != 0)
				SelectDrawBox(DrawBoxList[0]);
		}

		public void SelectDrawBox(DrawBox selected)
		{
			if (selected != null)
			{
				if (!DrawBoxList.Contains(selected))
				{
					Debug.AddExceptionInClass(this.GetType(), "SelectDrawBox", "Selected drawbox is not part of this container");
					return;
				}

				SelectedDrawBox = selected;
				selected.Activated = true;
			}
			else
				SelectedDrawBox = null;

			foreach (DrawBox box in DrawBoxList)
			{
				if (selected != box)
					box.Activated = false;
			}
		}

		public void SelectNone()
		{
			SelectDrawBox(null);
		}

		public void SelectNext()
		{
			int index = DrawBoxList.IndexOf(SelectedDrawBox);

			index++;

			if (index >= DrawBoxList.Count)
				index = 0;

			SelectedDrawBox = DrawBoxList[index];
		}

		public override ViewRect GetDefaultBoundaries(int newWidth, int newHeight)
		{
			return new ViewRect(RealX, RealY, newWidth, newHeight);
		}

		public override void Idle(Microsoft.Xna.Framework.GameTime gameTime)
		{
			base.Idle(gameTime);

			SelectDrawBox(SelectedDrawBox);
		}

		public override void Draw(Microsoft.Xna.Framework.GameTime gameTime, ViewRect viewRect)
		{
			base.Draw(gameTime, viewRect);
		}
	}
}
